home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.5 Applications 2002 November
/
SGI IRIX 6.5 Applications 2002 November.iso
/
dist
/
print.idb
/
usr
/
sbin
/
mknetpr.z
/
mknetpr
Wrap
Text File
|
1998-05-04
|
16KB
|
597 lines
#!/bin/sh
#Tag 0x00000700
#**************************************************************************
#*
#* Copyright (c) 1993 Silicon Graphics, Inc.
#* All Rights Reserved
#*
#* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
#*
#* The copyright notice above does not evidence any actual of intended
#* publication of such source code, and is an unpublished work by Silicon
#* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
#* the property of Silicon Graphics, Inc. Any use, duplication or
#* disclosure not specifically authorized by Silicon Graphics is strictly
#* prohibited.
#*
#* RESTRICTED RIGHTS LEGEND:
#*
#* Use, duplication or disclosure by the Government is subject to
#* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
#* Technical Data and Computer Software clause at DFARS 52.227-7013,
#* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
#* Supplement. Unpublished - rights reserved under the Copyright Laws of
#* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
#* Shoreline Blvd., Mountain View, CA 94039-7311
#**************************************************************************
#*
#* File: mknetpr
#*
#* $Revision: 1.26 $
#*
#* Description: Shell script for adding a remote printer to the local
#* System V spooling system.
#*
#* Usage: mknetpr [<local printer name>] [<remote host>]
#* [<remote printer name>]
#*
#* If the local printer name, remote printer name and remote
#* printer host are all specified on the command line, mknetpr does
#* not prompt for input and terminates upon encountering errors. If the
#* command line information is not complete, mknetpr runs interactively
#* prompting for the missing information and allowing recovery from
#* certain installation error conditions.
#*
#**************************************************************************
# Well known directories and programs
LPUTIL_DIR=/usr/lib
LPUTIL=$LPUTIL_DIR/lputil
OUT_FILT="/usr/bin/pg -s -p more... -e"
SPOOL_DIR=/var/spool/lp
LPSTAT=/usr/bin/lpstat
MKJTPR=/usr/sbin/mkjtpr
MKBSDPR=/usr/sbin/mkbsdnetpr
AWK_DIR=/usr/bin
AWK=$AWK_DIR/nawk
# Global variables
LOCAL_PNAME=""
REMOTE_PNAME=""
REMOTE_HOST=""
INTERACT=0
REMOTE_ID="lp"
#
# Prints the program usage string
#
PrintUsage()
{
echo "Usage: mknetpr [<local printer name>] [<remote host>] \c"
echo "[<remote printer name>]"
}
#
# Checks that we are the super-user and verifies
# that the System V spooling system is present
#
CheckPermSpooler()
{
idstr=`id`
cid=`expr "$idstr" : '^[ ]*uid=.*(\(.*\))[ ]*gid='`
if [ "$cid" != "root" ]; then
echo "You must be logged in as root to use this program."
exit 1;
fi
if [ ! -r $SPOOL_DIR ]; then
echo "The System V spooling system is not installed on this system."
exit 1
fi
}
#
# Check for a duplicate printer name. Returns 0 if dup,
# 1 if name is unique
#
CheckDupName()
{
/bin/ls $SPOOL_DIR/member/$1 2> /dev/null 1> /dev/null
return $?
}
#
# Query user for a name for the local printer
#
QueryLocalName()
{
while [ "$LOCAL_PNAME" = "" ]; do
echo "Enter new printer name (14 chars max.): \c"
read LOCAL_PNAME
VerifyPname $LOCAL_PNAME
if [ $? -eq 1 ]; then
echo "Invalid response: Name must be 14 chars (A-Za-z0-9_ only)"
LOCAL_PNAME=""
fi
if [ "$LOCAL_PNAME" = "" ]; then
continue
fi
CheckDupName $LOCAL_PNAME
if [ $? -eq 0 ]; then
echo " "
echo "WARNING: Printer $LOCAL_PNAME already exists."
echo '\nChoose a new name (y/n)[y]? \c'
read yn
if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
LOCAL_PNAME=""
fi
fi
done
}
#
# Test for pure decimal positive integer input
#
# Expects $1 to be the value to check. Returns 0 if
# OK, 1 if invalid positive integer or if more than one
# argument specified
#
VerifyInt()
{
if [ $# -eq 0 ]; then
return 0
fi
if [ $# -gt 1 ]; then
return 1
fi
echo $1 | egrep -s '^[0-9]*$'
return $?
}
#
# Test for a valid printer name (i.e. 14 chars max, A-Za-z0-9_
# only and no spaces)
#
VerifyPname()
{
if [ $# -eq 0 ]; then
return 0
fi
if [ $# -gt 1 ]; then
return 1
fi
echo $1 | egrep -s '^[A-Za-z0-9_]*$'
if [ $? -eq 1 ]; then
return 1
fi
if [ `expr $1 : '.*'` -gt 14 ]; then
return 1
fi
return 0
}
#
# Test for a properly constructed remote host name
#
VerifyHostname()
{
if [ $# -gt 1 ]; then
return 1
fi
return 0
}
#########################################################################
#
# Main program
#
#
# Ensure that we are root and that the main System V
# spooling system directory is present
#
CheckPermSpooler
#
# Get command line arguments, if any, and determine if we
# should run in interactive mode
#
case $# in
0) INTERACT=1
;;
1) INTERACT=1
LOCAL_PNAME=$1
;;
2) INTERACT=1
LOCAL_PNAME=$1
REMOTE_HOST=$2
;;
3) INTERACT=0
LOCAL_PNAME=$1
REMOTE_HOST=$2
REMOTE_PNAME=$3
;;
*) PrintUsage
exit 1
;;
esac
#
# Display an intro message
#
echo "Remote Printer Installation Tool\n"
# Ask user if this printer is directly on the network. We handle that
# with mkjtpr. ONLY ask if INTERACTive is false because this script
# can be called from the printer manager with all required arguments
# and in that case it should not query a user for info. Test for mkjtpr
# because it is part of Impressario and might not be installed (skip
# this if not installed).
if [ "$INTERACT" != "0" ]; then
while [ "$NETPRINTER" = "" ]; do
echo "Spool files:"
echo ""
echo "1. To another SGI workstation?"
echo " "
echo "2. A printer connected to a non-SGI workstation that supports the "
echo " BSD printing protocols (files will be converted to PostScript"
echo " before being sent to a BSD print spooler)?"
echo " "
if [ -x $MKJTPR ]; then
echo "3. Directly to a network printer. A network printer is a printer"
echo " connected to the network with a network card or network adaptor"
echo " (i.e. an HP JetDirect card)?"
echo " "
fi
echo "Enter selection:\c"
read NETPRINTER
VerifyInt $NETPRINTER
if [ $? -eq 1 ]; then
echo "Invalid response."
NETPRINTER=""
fi
if [ "$NETPRINTER" = "" ]; then
continue
fi
if [ \( "$NETPRINTER" != "1" \) -a \( "$NETPRINTER" != "2" \) -a \( "$NETPRINTER" != "3" \) ]; then
echo "Invalid response."
NETPRINTER=""
fi
if [ "$NETPRINTER" = "3" ]; then
if [ -x $MKJTPR ]; then
$MKJTPR "$LOCAL_PNAME" "$REMOTE_HOST"
exit $?
else
echo "Invalid response."
NETPRINTER=""
continue
fi
fi
if [ "$NETPRINTER" = "2" ]; then
$MKBSDPR "$LOCAL_PNAME" "$REMOTE_HOST"
exit $?
fi
done
fi
#
# Make sure we have a remote printer host
#
if [ "$REMOTE_HOST" = "" ]; then
while [ "$REMOTE_HOST" = "" ]; do
echo "Enter remote host machine name: \c"
read REMOTE_HOST
VerifyHostname $REMOTE_HOST
if [ $? -eq 1 ]; then
echo "Invalid response: Improperly constructed hostname"
REMOTE_HOST=""
fi
done
echo " "
fi
#
# First test that we can talk to the remote host
#
echo "Testing connection to remote host $REMOTE_HOST...\n"
# LPUTIL chkremote runs /usr/lib/print/listprinters
TMP_NAMES=`$LPUTIL chkremote "$REMOTE_HOST" "$REMOTE_ID"`
if [ $? -ne 0 ]; then
echo "Failed to connect to $REMOTE_HOST"
echo "Check that remote machine is up and allows login as $REMOTE_ID"
echo "You may need to run \"addclient(1M)\" on $REMOTE_HOST\n"
exit 1
fi
# Depending on the version of the lp system on the remote SGI.
# At this point, TMP_NAMES may be:
#
# 5N\n
# 5M\n
# dumb\n
# plotter\n
#
# Or it may be:
#
# twister MonoPostScript remote thurgood twister sgi
# tornado Raster remote thurgood tornado sgi
# 5N Raster local
# 5M MonoPostScript local
# testbsd PostScript remote imprtest2 raw bsd
# dumb PostScript local
# plotter ColorRaster local
# torn Raster remote thurgood tornado sgi
#
# Get just the first field from each line (which is the printer name)
REMOTE_NAMES=`echo "$TMP_NAMES" | $AWK '{print $1}'`
#
# Parse out the remaining data (if it is there). Missing fields become "<unknown>".
# After this you would have (note how the "<unknowns> keep evrything allinged
# so element 5 in the remote names list is correct for element 5 in all lists):
#
# twister tornado 5N 5M testbsd dumb plotter torn
# MonoPostScript Raster Raster MonoPostScript PostScript PostScript ColorRaster Raster
# remote remote local local remote local local remote
# thurgood tornado <unknown> <unknown> imprtest2 <unknown> <unknown> thurgood
# ...
#
REMOTE_TYPES=`echo "$TMP_NAMES" | $AWK '{if ($2!="") print $2; else print "<unknown>";}'`
REMOTE_CONNECTIONS=`echo "$TMP_NAMES" | $AWK '{if ($3!="") print $3; else print "<unknown>";}'`
REMOTE_TRUE_HOSTS=`echo "$TMP_NAMES" | $AWK '{if ($4!="") print $4; else print "<unknown>";}'`
REMOTE_TRUE_NAMES=`echo "$TMP_NAMES" | $AWK '{if ($5!="") print $5; else print "<unknown>";}'`
REMOTE_CONNTYPES=`echo "$TMP_NAMES" | $AWK '{if ($6!="") print $6; else print "<unknown>";}'`
#
# Now make sure there are printers on the remote host
#
if [ "$REMOTE_NAMES" = "" ]; then
echo "There are either no printers connected to $REMOTE_HOST"
echo "or the network connection to $REMOTE_HOST has failed. Check"
echo "that the remote machine is up and has at least one printer"
echo "connected."
exit 1
fi
echo "Connection to $REMOTE_HOST verified\n"
#
# Query user for a name for this printer
#
if [ "$LOCAL_PNAME" = "" ]; then
QueryLocalName
echo " "
else
echo "Requested local printer name: $LOCAL_PNAME\n"
VerifyPname $LOCAL_PNAME
if [ $? -eq 1 ]; then
echo "Error: Local printer name must be 14 chars (A-Za-z0-9_ only)"
if [ $INTERACT -eq 1 ]; then
LOCAL_PNAME=""
QueryLocalName
echo " "
else
exit 1
fi
fi
CheckDupName $LOCAL_PNAME
if [ \( $? -eq 0 \) -a \( $INTERACT -eq 1 \) ]; then
echo " "
echo "WARNING: Printer $LOCAL_PNAME already exists."
echo '\nChoose a new name (y/n)[y]? \c'
read yn
if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
LOCAL_PNAME=""
QueryLocalName
echo " "
fi
fi
fi
#
# Now select the remote printer
#
if [ "$REMOTE_PNAME" = "" ]; then
echo "Printers on $REMOTE_HOST are:"
nump=0
for pname in $REMOTE_NAMES
do
nump=`expr $nump + 1`
echo "\t$nump. $pname"
done
rnum=""
while [ "$rnum" = "" ]; do
echo "Enter number of printer to install: \c"
read rnum
VerifyInt $rnum
if [ $? -eq 1 ]; then
echo "Invalid response: Printer number must be between 1 and $nump"
rnum=""
fi
if [ "$rnum" = "" ]; then
continue
fi
if [ \( $rnum -eq 0 \) -o \( $rnum -gt $nump \) ]; then
echo "Invalid response: Number must be between 1 and $nump"
rnum=""
fi
done
cnt=0
for pname in $REMOTE_NAMES
do
cnt=`expr $cnt + 1`
if [ $cnt -eq $rnum ]; then
REMOTE_PNAME=$pname
break
fi
done
# Check to see if the remote printer is itself a network printer. If so,
# determine who the true server is and save it.
cnt=0
TRUE_REMOTE_HOST=""
for pname in $REMOTE_TRUE_HOSTS
do
cnt=`expr $cnt + 1`
if [ $cnt -eq $rnum ]; then
if [ $pname != "<unknown>" ]; then
TRUE_REMOTE_HOST=$pname
fi
break
fi
done
# Get the real remote printer name if required. Example:
# host: thurgood printer: test1 but test1 is a network printer to
# the host lodi printer 5MP. In above block we re-mapped thurgood
# to lodi and now we remap test1 to 5MP.
cnt=0
TRUE_REMOTE_PNAME=""
for pname in $REMOTE_TRUE_NAMES
do
cnt=`expr $cnt + 1`
if [ $cnt -eq $rnum ]; then
if [ $pname != "<unknown>" ]; then
TRUE_REMOTE_PNAME=$pname
fi
break
fi
done
cnt=0
CONNTYPE=""
for pname in $REMOTE_CONNTYPES
do
cnt=`expr $cnt + 1`
if [ $cnt -eq $rnum ]; then
if [ $pname != "<unknown>" ]; then
TRUE_CONNTYPE=$pname
fi
break
fi
done
if [ $TRUE_REMOTE_HOST ]; then
if [ $TRUE_REMOTE_PNAME ]; then
echo ""
echo "Warning: You are configuring a multi-hop printer."
echo "The printer $REMOTE_PNAME on host $REMOTE_HOST actually sends print jobs"
echo "to the printer $TRUE_REMOTE_PNAME on host $TRUE_REMOTE_HOST (resulting in"
echo "multiple hops to get to the actual print server). It would probably be"
echo "best if the printer you are configuring ($LOCAL_PNAME) also sent print"
echo "jobs directly to the printer $TRUE_REMOTE_PNAME on host $TRUE_REMOTE_HOST."
echo ""
echo "Do you want the print jobs for the new printer $LOCAL_PNAME sent to the"
echo "printer $TRUE_REMOTE_PNAME on host $TRUE_REMOTE_HOST (y/n)[y]? \c'"
read yn
if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
REMOTE_PNAME="$TRUE_REMOTE_PNAME"
REMOTE_HOST="$TRUE_REMOTE_HOST"
CONNTYPE="$TRUE_CONNTYPE"
fi
fi
fi
else
foundit=0
for pname in $REMOTE_NAMES
do
if [ "$pname" = "$REMOTE_PNAME" ]; then
foundit=1
break
fi
done
if [ $foundit -eq 0 ]; then
echo "Remote printer $REMOTE_PNAME not found on $REMOTE_HOST"
echo "Known printers on $REMOTE_HOST are:\n"
echo "$REMOTE_NAMES\n"
exit 1
fi
fi
#
# Inform the user of what comes next
#
echo " "
echo "Installing remote printer: $LOCAL_PNAME"
echo "Remote print server used: $REMOTE_HOST"
echo "Remote spooler name used: $REMOTE_PNAME"
echo " "
#
# Get the type and model name of the remote printer
#
##rinfo=`$LPUTIL remoteinfo $REMOTE_HOST $REMOTE_PNAME $REMOTE_ID`
##
##if [ \( $? -ne 0 \) -o \( "$rinfo" = "" \) ]; then
## echo "Could not get remote printer information"
## echo "Check network connection and printer installation on remote host"
## exit 1
##fi
### Change IFS to parse newlines as separators.
##oldIFS=$IFS
##IFS='
##'
##for item in $rinfo
##do
## val=`echo $item | egrep '^NAME='`
## if [ "$val" != "" ]; then
## NFLAG=$val
## else
## val=`echo $item | egrep '^TYPE='`
## if [ "$val" != "" ]; then
## TFLAG=$val
## fi
## fi
##done
##IFS=$oldIFS
#
# Install the printer.
#
if [ CONNTYPE ]; then
$LPUTIL addnet $REMOTE_HOST $REMOTE_PNAME $LOCAL_PNAME $CONNTYPE
else
$LPUTIL addnet $REMOTE_HOST $REMOTE_PNAME $LOCAL_PNAME
fi
if [ $? -ne 0 ]; then
echo "ERROR: Installation of new printer failed."
echo " $LPUTIL command failed."
exit 1
fi
#
# Inform the user of what has happened
#
echo "Remote printer $LOCAL_PNAME has been installed"
echo " "
echo "Here is your printing environment:"
echo " "
$LPSTAT -t | $OUT_FILT
#
# Return a successful exit code
#
exit 0